﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CRMSimple.LogService.Configuration.FileConfigurationExtension.Json;
using CRMSimple.LogService.Configuration.SQLDBConfiguration;
using BitAuto.EP.CRMLogSystem.BLL;
using BitAuto.EP.CRMLogSystem.Utils;
using BitAuto.EP.CRMLogSystem.Entity;
using Newtonsoft.Json.Linq;
using BitAuto.EP.FuncPMonitor.Aops;
namespace CRMSimple.LogServices
{
    public class ConfigurationManager
    {
        public readonly string AppSettingsName=AppConfig.GetAppSetting("AppSettingsName") ?? "appSettings.json";
        public ConfigurationManager()
        {
            LoadAppSettings();
        }
        public readonly string AppSettingsPath = AppConfig.GetAppSetting("AppSettingsPath");//从配置文件读取

        private static readonly string __key = "SQLDBConfigurationRootKey";

        private object obj = new object();
        public string this[string key, string filed,string type="json"]
        {
            get {
                switch (type)
                {
                    case "json":
                        return appSettings.ContainsKey(key) ? appSettings[key][filed] : string.Empty;
                    default:
                        return string.Empty;
                }   
            }

            set { 
                //回写配置文件
                switch (type)
                {
                    case "json":
                        if (appSettings.ContainsKey(key))
                        {
                            lock (obj)
                            {
                                appSettings[key][filed] = value;
                                this.WriteJsonConfig(appSettings[key]);
                            }
                        }
                        else
                        {
                            appSettings[key] = this.LoadJsonConfig(new JsonConfigurationRoot() { Path = this.GetAppSettingsPath(key) });
                            appSettings[key][filed] = value;
                        }

                        break;
                    default:
                        break;
                }


            }
        }
        public JObject this[string key]
        {
            get
            {
                return appSettings.ContainsKey(key) ? appSettings[key].Json : null;
            }
        }
        /// <summary>
        /// appSettings集合
        /// </summary>
        private IDictionary<string, JsonConfigurationRoot> appSettings;

        public IDictionary<string, JsonConfigurationRoot> AppSettings{get{return appSettings;}}
        /// <summary>
        /// 数据库加载的配置
        /// </summary>
        private IList<LogTableInfo> _SQLDBConfiguration;
         [FuncPMonitor()]
        public IList<LogTableInfo> SQLDBConfiguration
        {
            get {
                //加缓存
                _SQLDBConfiguration = CacheHelper.Get<IList<LogTableInfo>>(__key);
                if (_SQLDBConfiguration == null)
                {
                    try
                    {
                        _SQLDBConfiguration = bll.GetConfiguration();
                    }
                    catch (Exception)
                    {
                        
                        throw;
                    }
                    
                    CacheHelper.Set<IList<LogTableInfo>>(__key, _SQLDBConfiguration, EConvert.ToInt(AppConfig.GetAppSetting("CommonCacheTime")));
                }
                return _SQLDBConfiguration;
            }
        }

        private readonly SQLDBConfigurationBLL bll = new SQLDBConfigurationBLL();

        private JsonConfigurationRoot jsonConfigTemplate;
        /// <summary>
        /// 初始化加载的模版示例
        /// </summary>
        public JsonConfigurationRoot JsonConfigTemplate
        {
            get { 
                if(jsonConfigTemplate==null)
                {
                   var lazy= new Lazy<JsonConfigurationRoot>(()=>{
                       return new JsonConfigurationRoot() { Path = "Templates/appTemplate.json" };
                   },true);
                   jsonConfigTemplate=lazy.Value;
                   try
                   {
                       jsonConfigTemplate.Load();
                   }
                   catch (Exception ex)
                   {
                       
                       throw;
                   }
                  
                }
                return jsonConfigTemplate;
            }
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void LoadAppSettings()
        {
            if (this.SQLDBConfiguration != null)
            {
                appSettings = new Dictionary<string, JsonConfigurationRoot>(this.SQLDBConfiguration.Count);

                int i = 0;
                foreach (var item in this.SQLDBConfiguration)
                {
                    appSettings.Add(item.LogTableId, this.LoadJsonConfig(new JsonConfigurationRoot() { Path = this.GetAppSettingsPath(item.LogTableId) }));
                    i++;
                }
            }


        }
    }
}
